package com.project.myxima.adapters;

import android.content.Context;
import android.graphics.Color;

import android.view.View;


import androidx.viewpager.widget.ViewPager;

import com.project.myxima.R;
import com.project.myxima.utils.LogUtils;

import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.ColorTransitionPagerTitleView;

import java.util.List;


public class IndicatorAdapter extends CommonNavigatorAdapter {

    private final List<String> mTitles;
    private OnIndicatorTabClickListener mOnTabClickListener;
    private ViewPager mViewPager;

    public IndicatorAdapter(Context context, ViewPager viewPager, List<String> titles) {
        mViewPager = viewPager;
        mTitles = titles;
    }

    @Override
    public int getCount() {//数据的个数
        if(mTitles != null){
            return mTitles.size();
        }
        return 0;
    }

    @Override
    public IPagerTitleView getTitleView(Context context, final int index) {
        //创建view
        ColorTransitionPagerTitleView colorTransitionPagerTitleView = new ColorTransitionPagerTitleView(context);
        //设置一般情况下的颜色为灰色
        colorTransitionPagerTitleView.setNormalColor(Color.parseColor("#aaffffff"));
        //设置选中情况下的颜色为黑色
        colorTransitionPagerTitleView.setSelectedColor(Color.parseColor("#ffffff"));
        //单位sp
        colorTransitionPagerTitleView.setTextSize(18);
        //设置要显示的内容
        colorTransitionPagerTitleView.setText(mTitles.get(index));

        //设置title的点击事件，这里的话，如果点击了title,那么就选中下面的viewPager到对应的index里面去
        //也就是说，当我们点击了title的时候，下面的viewPager会对应着index进行切换内容。
        colorTransitionPagerTitleView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //切换viewPager的内容，如果index不一样的话。

                if (mViewPager != null) {
                    mViewPager.setCurrentItem(index);
                }else {
                    LogUtils.d("指示器的适配器", "----->pager为空");
                }

            }
        });
        //把这个创建好的view返回回去
        return colorTransitionPagerTitleView;

        //这是指示器Demo的代码
        /*SimplePagerTitleView simplePagerTitleView = new ColorTransitionPagerTitleView(context);
        simplePagerTitleView.setNormalColor(Color.GRAY);
        simplePagerTitleView.setSelectedColor(Color.WHITE);
        simplePagerTitleView.setText(mTitles[index]);
        simplePagerTitleView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d("TAG","进入了指示器的点击事件");
                mOnTabClickListener.onTabClick(index);
            }
        });
        return simplePagerTitleView;*/
    }

    @Override
    public IPagerIndicator getIndicator(Context context) {
        LinePagerIndicator linePagerIndicator = new LinePagerIndicator(context);
        linePagerIndicator.setMode(LinePagerIndicator.MODE_WRAP_CONTENT);
        linePagerIndicator.setColors(Color.WHITE);
        return linePagerIndicator;
    }

    public void setOnIndicatorTabClickListener(OnIndicatorTabClickListener listener){
        this.mOnTabClickListener = listener;
    }

    public interface OnIndicatorTabClickListener{
        void onTabClick(int index);
    }

}
